<?php
/**
 * User: Jerry
 * Date: 2017/6/16
 * Time: 10:50
 */

namespace SDXApi\Service;


/**
 * Class OssUploadService.class
 * @author Jerry
 * @package SDXApi\Service
 */
class OssUploadService
{
    protected $accessKeyId;
    protected $accessKeySecret;
    protected $host;
    protected $path;
    protected $fileName;

    public function __construct()
    {
        $this->accessKeyId = 'LTAIBq7QP4wZhHl2';
        $this->accessKeySecret = 'wUiHAoGwGQaU7D6DgO8pW81e3FB1AS';
        $this->host = 'http://oss.shidexian.com';

    }

    /**
     * oss直传签名获取（https://help.aliyun.com/document_detail/31926.html）
     * @param string $path
     * @param string $fileName
     * @return array
     */
    public function getToken($path='', $fileName='')
    {
        $this->path = $path;
        $this->fileName = $fileName;
        $response = $this->handTokenData($this->accessKeyId, $this->accessKeySecret, $this->host);

        return $response;
    }

    /**
     * @author Jerry
     * @param int $end
     * @return string
     */
    static protected function getISO8601($end = 0)
    {
        $dStr = date("c", $end);
        $myDataTime = new \DateTime($dStr);
        $expiration = $myDataTime->format(\DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        $expiration = substr($expiration, 0, $pos);
        return $expiration . "Z";
    }

    /**
     * @author Jerry
     * @return array
     */
    protected function getPathData()
    {

//      最大文件大小
        $condition = array(0 => 'content-length-range', 1 => 0, 2 => 1048576000);
        $conditions[] = $condition;

//      表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
        $path = $this->path;
        $start = array(0 => 'starts-with', 1 => '$key', 2 => $path);

        return $start;
    }

    /**
     * @author Jerry
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @param string $host
     * @return array
     */
    protected function handTokenData($accessKeyId = '', $accessKeySecret = '', $host = '')
    {
        $time = time();
        $expire = 3000;
        $end = $time + $expire;

        $expiration = self::getISO8601($end);
        $start = $this->getPathData($accessKeySecret);

        $base64_policy = self::getBase64Policy($start, $expiration);
        $string_to_sign = $base64_policy;

        $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $accessKeySecret, true));

        $fileName = $this->fileName ? $this->fileName : uniqid('shidexian_');



        $response = array();
        $response['accessid'] = $accessKeyId;
        $response['host'] = $host;
        $response['policy'] = $base64_policy;
        $response['signature'] = $signature;
        $response['expire'] = $end;
        $response['dir'] = $start[2];
        $response['fileName'] = $fileName;

        return $response;
    }

    /**
     * @author Jerry
     * @param array $start
     * @param int $expiration
     * @return string
     */
    static public function getBase64Policy($start = array(), $expiration = 0)
    {
        $conditions[] = $start;
        $arr = array('expiration' => $expiration, 'conditions' => $conditions);

        $policy = json_encode($arr);
        return base64_encode($policy);
    }


}